import { reqLogin, reqRegister, reqGetlang, reqSetlang, reqLogout, reqUserinfo } from '@/api/user'

export default {
  state: {
    userinfo: {}
  },
  mutations: {
    initUserInfo(state, data) {
      state.userinfo = data
    }
  },
  actions: {
    async login({ commit }, data) {
      const result = await reqLogin(data)
      if (result.code === 200) {
        localStorage.setItem('assets_token_order', result.token)
      } else {
        return result.code
      }
    },
    async register({ commit }, data) {
      const result = await reqRegister(data)
      if (result.code === 200) {
      } else {
        return result.code
      }
    },
    async getUserinfo({ commit }, data) {
      const result = await reqUserinfo(data)
      if (result.code === 200) {
        commit('initUserInfo', result.data)
      } else {
        return result.message
      }
    },
    async logout({ commit }, data) {
      const result = await reqLogout(data)
      if (result.code === 200) {
        localStorage.removeItem('assets_token_order')
        return result.code
      } else {
        return result.message
      }
    },
    async getLang({ commit }, data) {
      const result = await reqGetlang(data)
      if (result.code === 200) {
        return result
      } else {
        return result
      }
    },
    async setLang({ commit }, data) {
      const result = await reqSetlang(data)
      if (result.code === 200) {
      } else {
        return result.code
      }
    }
  },
  getters: {}
}
